package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.UserService;
import com.tarena.lbs.common.basic.converter.UserGroupConverter;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.param.UserParam;
import com.tarena.lbs.pojo.basic.param.UserTagsParam;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/basic/user/info/register")
    public Result<Void> saveUser(@RequestBody UserParam param) throws BusinessException {
        //调用业务层 新增注册
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(param,userBO);
        userService.saveUser(userBO);
        return Result.success();
    }
    //给用户绑定标签
    @GetMapping("/basic/user/tag")
    public Result<Void> BindUserTags(UserTagsParam param) throws BusinessException {
        userService.bindUsertags(param);
        return Result.success();
    }

    @Autowired
    private UserGroupConverter userGroupConverter;
    @PostMapping("/admin/basic/userGroup/list")
    public Result<PageResult<UserGroupVO>> userGroupPage(@RequestBody UserGroupQuery query) throws BusinessException{
        BasePageList<UserGroupBO> boPage = userService.userGroupPage(query);
        return new Result<>(userGroupConverter.assemblePageVOS(boPage,query));
    }

    //新增用户人群
    @PostMapping("/admin/basic/userGroup/save")
    public Result<Void> saveUserGroup(@RequestBody UserGroupParam userGroupParam)
            throws BusinessException{
        UserGroupBO bo=new UserGroupBO();
        BeanUtils.copyProperties(userGroupParam,bo);
        userService.saveUserGroup(bo);
        return Result.success();
    }
}
